package com.wd.live.ui.activity

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.wd.live.BaseActivity
import com.wd.live.BaseConstant
import com.wd.live.R
import com.wd.live.databinding.ActivityLoginBinding
import com.wd.live.tool.LoginHelper
import com.wd.live.tool.longToast
import com.wd.live.tool.lunchActivity
import com.wd.live.tool.saveLoginInfo
import com.wd.live.tool.viewBinding
import com.wd.live.viewmodel.LoginViewModel
import java.lang.ref.WeakReference

/**
 * 登陆，微信登陆
 */
class LoginActivity : BaseActivity() {
    private var wxApi: IWXAPI? = null
    private val binding: ActivityLoginBinding by viewBinding()
    private val viewModel: LoginViewModel by viewModels()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.openWx.setOnClickListener {
            if (binding.checkbox.isChecked) {
                wxApi?.let { it1 -> viewModel.getWxCode(it1) }
            } else {
                longToast("请先同意协议")
            }
        }
        buildAgreeView()
        viewModel.userInfo.observe(this) {
            try {
                longToast("登陆成功")
                //保存数据
                saveLoginInfo(it)
                //缓存一下登陆数据
                BaseConstant.userId = it.userId
                BaseConstant.sessionId = it.sessionId
                lunchActivity<MainActivity>()
                finish()
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }
        initWx()
    }

    private fun buildAgreeView() {
        val checkText = binding.checkText.text
        val startIndex = checkText.indexOf("《")
        val endIndex = checkText.indexOf("》") + 1

        val sBuilder = SpannableStringBuilder(checkText)
        val clickableSpan: ClickableSpan = object : ClickSpan(this) {
            override fun onClick(widget: View) {
                lunchActivity<AgreementActivity>()
            }
        }
        sBuilder.setSpan(
            clickableSpan,
            startIndex,
            endIndex,
            Spanned.SPAN_INCLUSIVE_EXCLUSIVE
        )
        sBuilder.setSpan(
            ForegroundColorSpan(Color.parseColor("#FAA08F")),
            startIndex,
            endIndex,
            Spanned.SPAN_INCLUSIVE_EXCLUSIVE
        )
        binding.checkText.text = sBuilder
        binding.checkText.movementMethod = LinkMovementMethod.getInstance()

    }

    override fun onResume() {
        super.onResume()
        LoginHelper.getWxLoginCode(applicationContext)?.let {
            viewModel.doLogin(it)
        }
    }

    private fun initWx() {
        //初始化微信
        wxApi = WXAPIFactory.createWXAPI(this, BaseConstant.WX_APP_ID, false)
        wxApi?.registerApp(BaseConstant.WX_APP_ID)
    }


    internal abstract class ClickSpan(context: Context) :
        ClickableSpan() {
        private val reference: WeakReference<Context>

        init {
            reference = WeakReference(context)
        }

        override fun updateDrawState(ds: TextPaint) {
            ds.color = ContextCompat.getColor(reference.get()!!, R.color.agreement_color)
            ds.isUnderlineText = false
            ds.clearShadowLayer()
        }
    }
}